//
//  PagerScrollView.swift
//  Example-Swift
//
//  Created by samuel on 2019/4/5.
//  Copyright © 2019 samuel. All rights reserved.
//

import UIKit

open class PagerScrollView: UIScrollView, UIGestureRecognizerDelegate {
    
    private var otherGestureRecognizers: [UIGestureRecognizer]?
    
    public init(frame: CGRect, otherGestureRecognizers: [UIGestureRecognizer]? = nil) {
        self.otherGestureRecognizers = otherGestureRecognizers
        super.init(frame: frame)
        autoresizingMask = [.flexibleWidth, .flexibleHeight]
        isScrollEnabled = true
        isPagingEnabled = false
        backgroundColor = .white
        alwaysBounceVertical = true
        showsVerticalScrollIndicator = false
        showsHorizontalScrollIndicator = false
    }
    
    public required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
        
//        override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
//            var v = super.point(inside: point, with: event)
//            print("+aaa+22++oooo\(v)")
//
//    //        if !v{
//    //            if let vs = progressBar{
//    //                let tp = vs.convert(point, from: self)
//    //                if bounds.contains(tp){
//    //                    v = vs.atTab
//    //                }
//    //            }
//    //        }
//            return v
//
//        }
//        //
//        override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
//            var v = super.hitTest(point, with: event)
//            print("+aa+++oooo\(v)")
//    //        if v == nil{
//    //            if let vs = progressBar{
//    //                let tp = vs.convert(point, from: self)
//    //                if bounds.contains(tp){
//    //                    v = vs
//    //                }
//    //            }
//    //        }
//            return v
//        }
    
    open func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        guard let _ = gestureRecognizer.view as? UIScrollView else {
            return false
        }
        
        if let otherGestureRecognizers = otherGestureRecognizers, otherGestureRecognizers.contains(otherGestureRecognizer) {
            return false
        }
        return true
    }
}

private var markAsScrollableKey: Void?
extension UIScrollView {
    
    public var markAsScrollable: Bool {
        get {
            return (objc_getAssociatedObject(self, &markAsScrollableKey) as? Bool) ?? false
        }
        set {
            objc_setAssociatedObject(self, &markAsScrollableKey, newValue, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
        }
    }
}
